package com.good.msgsys.service.impl;

import com.good.msgsys.dao.UserDao;
import com.good.msgsys.entity.User;
import com.good.msgsys.service.UserService;
import com.good.msgsys.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author junlong
 * @date 2020/12/31 11:08
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private IdWorker idWorker;

    @Override
    public void add(User user) {
        // 生成id
        String id = idWorker.nextId() + "";
        user.setId(id);
        userDao.save(user);
    }

    @Override
    public void deleteById(String id) {
        userDao.deleteById(id);
    }

    @Override
    public void update(User user) {
        User temp = userDao.findById(user.getId()).get();
        temp.setName(user.getName());
        temp.setAge(user.getAge());
        userDao.save(temp);
    }

    @Override
    public User findById(String id) {
        return userDao.findById(id).get();
    }

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }
}
